package com.salon.modules.makeup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.salon.common.exception.CustomException;
import com.salon.common.http.response.PageResult;
import com.salon.modules.app.form.LoginForm;
import com.salon.modules.makeup.mapper.UserMapper;
import com.salon.modules.makeup.entity.UserEntity;
import com.salon.modules.makeup.service.UserService;
import com.salon.common.utils.Query;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.Map;

/**
 * 用户
 *
 * @author lch 12345678@qq.com
 * @since 1.0.0 2023-10-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Override
    public PageResult<UserEntity> listUserByPage(Map<String, Object> params) {
        IPage<UserEntity> page = new Query<UserEntity>().getPage(params);
        IPage<UserEntity> list = baseMapper.listUserByPage(page, params);
        return new PageResult<>(list.getTotal(), list.getCurrent(), list.getSize(), list.getRecords());
    }

    @Override
    public UserEntity queryByMobile(String mobile) {
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserEntity::getMobile, mobile);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public Long login(LoginForm form) {
        UserEntity user = queryByMobile(form.getMobile());
        if(ObjectUtils.isEmpty(user)) {
            throw new CustomException("手机号或密码错误");
        }
        if (!user.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))) {
            throw new CustomException("手机号或密码错误");
        }
        return user.getUserId();
    }

}